package cn.clx.ymcc.service.impl;

import cn.clx.ymcc.domain.Course;
import cn.clx.ymcc.feign.ICourseUserLearnFeignClient;
import cn.clx.ymcc.result.JSONResult;
import cn.clx.ymcc.service.IAlicloudOSSService;
import cn.clx.ymcc.util.AssertUtil;
import cn.clx.ymcc.util.StrUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class AlicloudOSSServiceImpl implements IAlicloudOSSService {

    @Autowired
    private ICourseUserLearnFeignClient courseUserLearnFeignClient;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     * 无重复token获取
     * @param id
     * @return
     */
    @Override
    public String createToken(String id) {
        JSONResult jsonResult = courseUserLearnFeignClient.get(Long.valueOf(id));
        AssertUtil.isTrue(jsonResult.isSuccess(),jsonResult.getMessage());
        AssertUtil.isNotNull(jsonResult.getData(),"该课程不存在");
        Course course = JSONObject.parseObject(jsonResult.getData().toString(), Course.class);
        AssertUtil.isEquals(course.getStatus().toString(),"1","该课程还未上架，还不可购买");
        //登录人写死  后面做了登录再来改
        Long LoginId=3l;
        String token = StrUtils.getComplexRandomString(8);
        redisTemplate.opsForValue().set(LoginId+":"+id,token,10, TimeUnit.MINUTES);
        return token;
    }
}
